import emitter from "./emitter";
import playerDataInfo from "./playerData";

const { ccclass, property } = cc._decorator;

@ccclass
export default class gameUI extends cc.Component {

    @property(cc.Label)
    txtCoin: cc.Label = null;
    @property(cc.Label)
    txtTiLi: cc.Label = null;
    @property(cc.Label)
    txtLevel: cc.Label = null;
    @property(cc.Slider)
    timeSlider: cc.Slider = null;
    @property(cc.ProgressBar)
    timeProgress: cc.ProgressBar = null;
    @property(cc.Animation)
    nodeLifeArr: cc.Animation[] = [];
    playerBlood = 3;
    @property(cc.Label)
    txtBulletLevel: cc.Label = null;

    onLoad() {
        //设置金币、体力和关卡数据
        //@ts-ignore
        this.txtCoin.string = playerDataInfo.gameData.coin;
        //@ts-ignore
        this.txtTiLi.string = playerDataInfo.gameData.tiLi;
        //@ts-ignore
        this.txtLevel.string = playerDataInfo.gameData.level;
        this.addEventListener();
        this.setProgress(60);
        //@ts-ignore
        this.setBlood(3 - playerDataInfo.gameData.lifeNum);
    }

    addEventListener() {
        emitter.on("gameStart", this.setGameState.bind(this));
        emitter.on("setBlood", this.setBlood.bind(this));
        emitter.on("setEnemyPlaneNumber", this.setEnemyPlaneNumber.bind(this));
        emitter.on("setBulletLevelAndType", this.setBulletLevelAndType.bind(this));
        emitter.on("gameOver", this.gameEnd.bind(this));
    }

    onDisable() {
        emitter.off("gameOver", this.gameEnd.bind(this));
        emitter.off("setBlood", this.setBlood.bind(this));
        emitter.off("setEnemyPlaneNumber", this.setEnemyPlaneNumber.bind(this));
        emitter.off("gameStart", this.setGameState.bind(this));
        emitter.off("setBulletLevelAndType", this.setBulletLevelAndType.bind(this));
    }

    //显示玩家子弹类型和等级
    setBulletLevelAndType(type, level) {
        this.txtBulletLevel.string = type + level;
    }

    //设置玩家血量
    setBlood(blood) {
        this.playerBlood -= blood;
        this.nodeLifeArr[this.playerBlood].play();
        if (this.playerBlood == 0) {
            this.unschedule(this.setGameTimeCallback);
            this.gameEnd("lose");
        }
    }

    //游戏结束
    gameEnd(state) {
        emitter.emit("gameEnd");
        //保存需要的数据 结算界面用
        //@ts-ignore
        window.gameData = {
            gameState: state,
            lifeNum: this.playerBlood,
            bulletType: this.txtBulletLevel.string[0],
            bulletLevel: this.txtBulletLevel.string[1],
            wipePlane: this.wipePlane
        }

        //@ts-ignore
        emitter.off();
        cc.director.loadScene("gameEnd");
    }

    //设置游戏boss时间
    setGameState(state) {
        this.setProgress(60);
    }

    setGameTimeCallback
    setProgress(time) {
        let self = this;
        this.setGameTimeCallback = () => {
            time -= 0.1;
            if (time > 0) {
                let progr = 1 / 60 / 10;
                self.timeSlider.progress += progr;
                self.timeProgress.progress += progr;
                self.setProgress(time);
            }
        }
        this.scheduleOnce(this.setGameTimeCallback.bind(this), 0.1)
    }

    wipePlane = 0;
    //玩家击杀敌机数量
    setEnemyPlaneNumber() {
        this.wipePlane += 1;
    }

}
